package com.ruoyi.web.controller.asset;


import com.ruoyi.asset.service.IMultipartFileService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/multi")
@Api(value = "MultipartFileController", tags = "文件上传与下载（记录file表）")
public class MultipartFileController {

    @Autowired
    private IMultipartFileService multipartFileService;

    /**
     * 上传文件并提供文件相关参数(资产新增前)
     *
     * @param file
     * @param req
     * @return
     */
    @ApiOperation(value = "上传文件并提供文件相关参数(资产新增前)")
    @PostMapping(value = "/uploadFile")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam Map req) {
        return multipartFileService.uploadFile(file, req);
    }

    /**
     * 上传文件并提供文件相关参数(资产新增后)
     *
     * @param file
     * @param req
     * @return
     */
    @ApiOperation(value = "上传文件并提供文件相关参数(资产新增后)")
    @PostMapping(value = "/uploadFileForOrder")
    public AjaxResult uploadFileForOrder(@RequestParam("file") MultipartFile file, @RequestParam Map req) {
        return multipartFileService.uploadFileForOrder(file, req);
    }

    /**
     * 通过文件id下载文件
     *
     * @param fileId
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "通过文件id下载文件")
    @GetMapping("/downloadFile/{fileId}")
    public void downloadFile(@PathVariable("fileId") Long fileId, HttpServletResponse response) throws IOException {
        multipartFileService.downloadFile(fileId, response);
    }

    /**
     * 通过文件id删除文件
     *
     * @return
     */
    @ApiOperation(value = "通过文件id删除文件")
    @GetMapping("/removeFile/{fileId}")
    @Log(title = "删除文件", businessType = BusinessType.DELETE)
    public AjaxResult removeFile(@PathVariable("fileId") Long fileId) {
        return multipartFileService.removeFile(fileId);
    }
}
